package com.itheima.skyserver.controller;

import com.itheima.skyserver.pojo.domain.Category;
import com.itheima.skyserver.result.PageResult;
import com.itheima.skyserver.result.Result;
import com.itheima.skyserver.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public Result page(String name, Integer page, Integer pageSize, Integer type) {
        PageResult pageResult = categoryService.page(name, page, pageSize, type);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status, Long id) {
        categoryService.setStatus(status, id);
        return Result.success();
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping()
    public Result insertCategory(@RequestBody Category category) {
        categoryService.insertCategory(category);
        return Result.success();
    }

    @DeleteMapping()
    public Result deleteCategoryById(Integer id) {
        categoryService.deleteCategoryById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(Integer type) {
        List<Category> categories = categoryService.list(type);
        return Result.success(categories);
    }

    @PutMapping()
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }
}
